﻿Public Class PRODINPROM
    Dim connect As CONDB = CONDB.NewConnection()
    Dim PROVIDER_Check As New PROVIDERSCLASS

    Private Sub PRODUCTINPROMOTION_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim objQueryCmbProid As New propriceoncontact
        objQueryCmbProid.ProoncontactCombobox(promoidPropriceCmb)

        Dim objPrdprice As New propriceoncontact
        Dim prdpriceData As DataTable
        prdpriceData = connect.GetTable(objPrdprice.selectPrdprice_)
        prdpriceGrid.PrimaryGrid.DataSource = prdpriceData

        changeDiscountType.Value = False
        changeDiscountType.Enabled = False

        DesignForm(Me)
        DesignGrid(prdpriceGrid)

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Private Sub switchProductType_ValueChanged(sender As Object, e As EventArgs) Handles switchProductType.ValueChanged
        If switchProductType.Value = True Then
            pckplistLabel.Visible = False
            pckpInProductipriceTxt.Visible = False

            plistLabel.Visible = True
            productlistInPrdpriceTxt.Visible = True

            changeDiscountType.Enabled = True
        ElseIf switchProductType.Value = False Then
            plistLabel.Visible = False
            productlistInPrdpriceTxt.Visible = False

            pckplistLabel.Visible = True
            pckpInProductipriceTxt.Visible = True

            changeDiscountType.Value = False
            changeDiscountType.Enabled = False
        End If
    End Sub

    Private Sub changeDiscountType_ValueChanged(sender As Object, e As EventArgs) Handles changeDiscountType.ValueChanged
        If changeDiscountType.Value = True Then
            percent1.Visible = False
            percent2.Visible = False
        ElseIf changeDiscountType.Value = False Then
            percent1.Visible = True
            percent2.Visible = True
        End If
    End Sub

    Private Sub productlistInPrdpriceTxt_TextChanged(sender As Object, e As EventArgs) Handles productlistInPrdpriceTxt.TextChanged
        pIdInProductprice.Text = productlistInPrdpriceTxt.Tag
        productType.Text = "ผลิตภัณฑ์"
    End Sub

    Private Sub pckpInProductipriceTxt_TextChanged(sender As Object, e As EventArgs) Handles pckpInProductipriceTxt.TextChanged
        pIdInProductprice.Text = pckpInProductipriceTxt.Tag
        productType.Text = "กลุ่มผลิตภัณฑ์"
    End Sub

    Private Sub submitPriceBtn_Click(sender As Object, e As EventArgs) Handles submitPriceBtn.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                If promoidPropriceCmb.SelectedValue.ToString.Length > 0 And pckpInProductipriceTxt.Text.ToString.Length > 0 And pIdInProductprice.Text.ToString.Length > 0 And productType.Text.ToString.Length > 0 Then
                    If productType.Text = "ผลิตภัณฑ์" Then
                        Dim objInsertData As New propriceoncontact
                        objInsertData.PROID_ = promoidPropriceCmb.SelectedValue     '1

                        Dim getPOCID As String
                        getPOCID = connect.GetTable("SELECT pocid FROM prooncontact WHERE proid = '" & promoidPropriceCmb.SelectedValue & "'").Rows(0)(0).ToString
                        'MsgBox(getPOCID)
                        objInsertData.POCID_ = getPOCID     '2

                        objInsertData.PRDCODE_ = pIdInProductprice.Text     '3

                        If changeDiscountType.Value = True Then
                            Dim getProductMainPriceOPD As Double
                            getProductMainPriceOPD = connect.GetTable("SELECT opdrc FROM masproduct WHERE prdcode = '" & pIdInProductprice.Text & "'").Rows(0)(0).ToString
                            Dim getProductMainPriceIPD As Double
                            getProductMainPriceIPD = connect.GetTable("SELECT ipdprc FROM masproduct WHERE prdcode = '" & pIdInProductprice.Text & "'").Rows(0)(0).ToString

                            Dim changeOPDPricetoPercent As Double
                            changeOPDPricetoPercent = (getProductMainPriceOPD * 100) / opdPrice.Text
                            Dim changeIPDPricetoPercent As Double
                            changeIPDPricetoPercent = (getProductMainPriceIPD * 100) / ipdPrice.Text

                            objInsertData.ODISCOUNT_ = changeOPDPricetoPercent       '4
                            objInsertData.IDISCOUNT_ = changeIPDPricetoPercent       '5
                        ElseIf changeDiscountType.Value = False Then
                            objInsertData.ODISCOUNT_ = opdPrice.Text    '4
                            objInsertData.IDISCOUNT_ = ipdPrice.Text    '5
                        End If

                        objInsertData.STATUS_ = proonproductStatusChk.Checked       '6

                        MsgBox("promotionid" & objInsertData.PROID_ & "POCID" & objInsertData.POCID_ & "productid" & objInsertData.PRDCODE_ & "opdprice" & objInsertData.ODISCOUNT_ & "ipdprice" & objInsertData.IDISCOUNT_ & "status" & objInsertData.STATUS_)

                        'objInsertData.insertProonproduct()

                        Dim objPrdprice As New propriceoncontact
                        Dim prdpriceData As DataTable
                        prdpriceData = connect.GetTable(objPrdprice.selectPrdprice_)
                        prdpriceGrid.PrimaryGrid.DataSource = prdpriceData

                        pIdInProductprice.Text = ""
                        productType.Text = ""
                        productlistInPrdpriceTxt.Text = ""
                        pIdInProductprice.Text = ""
                    ElseIf productType.Text = "กลุ่มผลิตภัณฑ์" Then

                    End If
                Else
                    MsgBox("โปรดกรุณาระบุข้อมูลให้ครบถ้วน")
                End If
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub
End Class